package com.anbinh.doctor.fragment.account;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.LanguageListAdapter;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ItemSpecial;
import com.anbinh.doctor.model.Language;
import com.anbinh.doctor.model.ResponseLanguage;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentAddNgonngu extends Fragment {
	private static FragmentAddNgonngu fragment = null;
	private View mView;
	private LanguageListAdapter adapter;
	private ListView mGridView;
	private CircleProgressBar loadding;
	private ItemSpecial currentDoctorInfo;
	private List<Language> mListSp;

	public static FragmentAddNgonngu newInstance() {
		if (fragment == null) {
			fragment = new FragmentAddNgonngu();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_thongtincanhan, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);

		mGridView = (ListView) mView.findViewById(R.id.listView1);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				for (int i = 0; i <= mListSp.size(); i++) {
					if (position == i) {
						mListSp.get(position).setChoosed(
								!mListSp.get(position).isChoosed());
					}
				}
				adapter.notifyDataSetChanged();

			}
		});

		onLoadData();
	}

	public void pushSpecialList() {
		String list = getListSpecial();
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		ServerController.getInstance(getActivity()).requestPushLanguage(token,
				list, new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
						Utils.showMessage(getActivity(), data.getDescription()
								+ " " + data.getResponsecode());
						Intent intent = new Intent();
						intent.putExtra(Utils.KEY, Utils.RESUME_EXTRA_INFO);
						getActivity().setResult(Integer.MAX_VALUE, intent);
						getActivity().finish();
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
					}

				});
	}

	private String getListSpecial() {
		String result = "";
		for (int i = 0; i < mListSp.size(); i++) {
			if (mListSp.get(i).isChoosed()) {
				result = mListSp.get(i).getId() + "," + result;
			}
		}
		if (result.isEmpty()) {
			return "";
		}
		return result.substring(0, result.length() - 1);
	}

	private void onLoadData() {
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		ServerController.getInstance(getActivity()).requestGetLanguage(token,
				new OnRequest<ResponseLanguage>() {

					@Override
					public void onSuccess(ResponseLanguage data) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
						if (data.getResponsecode() >= 0) {
							mListSp = data.getData();
							adapter = new LanguageListAdapter(getActivity(),
									R.layout.seprator_chuyen_khoa, mListSp, 0);
							mGridView.setAdapter(adapter);

						}
					}

					@Override
					public void onError(int errorCode, String message) {
						// TODO Auto-generated method stub

					}
				});
	}

	public ItemSpecial getCurrentDoctorInfo() {
		return currentDoctorInfo;
	}

}
